﻿using EasyAdmin.Application.Contracts.Systems.Roles;
using EasyAdmin.Application.Contracts.Systems.Roles.Dtos;
using EasyAdmin.Domain.Entities.Systems;
using EasyCore.Application;
using EasyCore.ObjectMapper;
using EasyCore.UnifyResult.Exceptions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Application.Systems
{
    /// <summary>
    /// 角色应用层管理
    /// </summary>
    public class RoleAppService : AppService, IRoleAppService
    {
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly ISqlSugarRepository<SysRole> _roleRepo;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleRepo"></param>
        public RoleAppService(ISqlSugarRepository<SysRole> roleRepo)
        {
            this._roleRepo = roleRepo;
        }

        /// <summary>
        /// 获得分页数据
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<SqlSugarPagedList<RoleOutput>> Page(RolePageCondition condition)
        {
            var data = await _roleRepo.AsQueryable().Select(m => new RoleOutput
            {
                Id = m.Id,
                RoleName = m.RoleName,
                RoleCode = m.RoleCode,
                OrderSort = m.OrderSort,
                IsDisabled = m.IsDisabled
            }).OrderByDescending(m => m.OrderSort).ToPagedListAsync(condition.PageIndex, condition.PageSize);
            return data;
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task CreateRole(CreateUpdateRoleDto input)
        {
            var role = await _roleRepo.FirstOrDefaultAsync(m => m.RoleCode == input.RoleCode && m.IsDeleted == false);
            if (role != null) BusinessThrow.Throw( "角色编码已存在");
            role = await _roleRepo.FirstOrDefaultAsync(m => m.RoleName == input.RoleName && m.IsDeleted == false);
            if (role != null) BusinessThrow.Throw("角色名称已存在");
            role = input.MapTo<SysRole>();
            await _roleRepo.InsertAsync(role);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateRole(CreateUpdateRoleDto input)
        {
            if (!input.Id.HasValue) BusinessThrow.Throw("标识不存在");
            var role = await _roleRepo.FirstOrDefaultAsync(m => m.RoleCode == input.RoleCode && m.IsDeleted == false && m.Id != input.Id.Value);
            if (role != null) BusinessThrow.Throw("角色编码已存在");
            role = await _roleRepo.FirstOrDefaultAsync(m => m.RoleName == input.RoleName && m.IsDeleted == false && m.Id != input.Id.Value);
            if (role != null) BusinessThrow.Throw("角色名称已存在");
            role = await _roleRepo.FirstOrDefaultAsync(m => m.Id == input.Id.Value);
            if (role == null) BusinessThrow.Throw("数据不存在");
            role = input.MapTo<CreateUpdateRoleDto, SysRole>(role);
            await _roleRepo.InsertAsync(role);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteRoleById(long roleId)
        {
            var role = await _roleRepo.FirstOrDefaultAsync(m => m.Id == roleId);
            if (role == null) BusinessThrow.Throw("数据不存在");
            await _roleRepo.DeleteAsync(role);
        }

        /// <summary>
        /// 禁止或启用角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task ChangeRoleStatusByRoleId(long roleId)
        {
            var role = await _roleRepo.FirstOrDefaultAsync(m => m.Id == roleId);
            if (role == null) BusinessThrow.Throw("数据不存在");
            role.IsDisabled = !role.IsDisabled;
            await _roleRepo.UpdateAsync(role);
        }    

        /// <summary>
        /// 获得所有角色
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<RoleOutput>> GetAllRoles()
        {
            var data = await _roleRepo.AsQueryable().Select(m => new RoleOutput
            {
                Id = m.Id,
                RoleName = m.RoleName,
                RoleCode = m.RoleCode,
                OrderSort = m.OrderSort,
                IsDisabled = m.IsDisabled
            }).OrderByDescending(m => m.OrderSort).ToListAsync();
            return data;
        }


        /// <summary>
        /// 获得角色详细信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
       public async Task<RoleOutput> GetRoleByRoleId(long roleId) {
            var role = await _roleRepo.FirstOrDefaultAsync(m => m.Id == roleId);
            if (role == null) BusinessThrow.Throw("数据不存在");
           return role.MapTo<RoleOutput>();
        }
    }
}
